package com.loooooo.pm2024.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Utils {

    static MessageDigest md;

    static {
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static String md5If(String text) {
        if (text == null) return text;
        //MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5 = md.digest(text.getBytes(StandardCharsets.UTF_8));
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < md5.length; offset++) {
            int i = md5[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();
    }
    public static byte[] md5(String text) {
        return md.digest(text.getBytes(StandardCharsets.UTF_8));

    }
    public static String md5If(byte [] text) {
        if (text == null || text.length==0) return null;
        byte[] md5 = md.digest(text);
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < md5.length; offset++) {
            int i = md5[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();

    }
    public static void main(String[] args) {
        //F1ABEA739FB49EB23D1FD0F69C7FC250
        String p = "iemydxiaodan1";
        p = md5If(p);//.toUpperCase();
        System.out.println(p);
        p = md5If(p+ "zhlm" ).toUpperCase();
        System.out.println(p);
    }
}
